var plib = require("./plib");
//带有超时删除机制的 速度结构 ， 
var ptArray = function()
{
	this._array = {}; //new Array();
	this._arraytime = {}; //new Array();
	this._timeout = 3000;
	this._checktime = 0;
}
ptArray.prototype.hasKey = function(key)
{
	if(key in this._array)
		return 0; 
	else 
		return 1;
}
ptArray.prototype.Get = function(key)
{
	this.Clean();
	if(key in this._array)
		return this._array[key.toString()];
	else 
		return 	null;
};

ptArray.prototype.Set = function(key, val)
{
	this.Clean();
	this._array[key] = val;
	this._arraytime[key] = plib.now();
	return 0;
};


ptArray.prototype.Delete = function(key)
{
	if(key in this._array) 
		delete this._array[key];
	if(key in this._arraytime)
		delete this._arraytime[key];
};

ptArray.prototype.Flush = function(key)
{
	if(key in this._array)
		this._arraytime[key] = plib.now();
	return 0;
};

ptArray.prototype.ForEach = function(_cb)
{
	this.Clean();
	for(var key in this._array)
	{
		_cb(this._array[key]);
	}
}
ptArray.prototype.Clean = function()
{
	var n = plib.now();
	for(k in this._array)
	{
		if(k  in this._arraytime)
		{
			if(n - this._arraytime[k] > this._timeout)
			{
				this.Delete(k);
			}
		} 
	}
	this._timeout = n;
};

module.exports.CreateArray = function(t)
{
	var a = new ptArray();
	a._timeout = t;
	return a;
};
